extends Node2D

@onready var btn1: TextureButton = $"Control/TextureButton"
@onready var btn2: TextureButton = $"Control/TextureButton2"
@onready var btn3: TextureButton = $"Control/TextureButton3"
var spacing: float = 20.0	# 圆点间距（用于直线白点）
var radius: float = 3.0	# 白色圆点半径（用于直线白点）

var use_arc: bool = false	# 是否使用 draw_arc 画弧线
var curvature: float = 100.0	# 弧线弯曲幅度（通过中点法线偏移）

# 动画参数：白点逐个出现
var animate: bool = true	# 是否启用逐点出现动画
var reveal_rate: float = 200.0	# 每秒显示的曲线像素长度
var loop_reveal: bool = false	# 完成后是否循环从头开始
var reveal_counter: float = 0.0	# 当前已显示的长度（像素）

func _ready() -> void:
	reveal_counter = 0.0
	queue_redraw()

func _process(_delta: float) -> void:
	if animate:
		reveal_counter += reveal_rate * _delta
	queue_redraw()

func _draw():
	if btn1 == null or btn2 == null:
		return

	var p1_global: Vector2 = btn1.global_position + btn1.size * 0.5
	var p2_global: Vector2 = btn2.global_position + btn2.size * 0.5
	var p1: Vector2 = to_local(p1_global)
	var p2: Vector2 = to_local(p2_global)

	var dir: Vector2 = p2 - p1
	var length := dir.length()
	if length <= 0.001:
		return

	if use_arc:
		# 优先使用第三个按钮作为二次贝塞尔的控制点；否则退回圆弧法
		if btn3 != null:
			var c_global: Vector2 = btn3.global_position + btn3.size * 0.5
			var c: Vector2 = to_local(c_global)
			var approx_len: float = p1.distance_to(c) + c.distance_to(p2)
			var steps: int = max(1, int(approx_len / spacing))
			var visible_len: float = min(reveal_counter, approx_len)
			var visible_steps: int = min(steps, int(visible_len / spacing))
			for i in range(visible_steps + 1):
				var t: float = float(i) / float(steps)
				var pos: Vector2 = ((1.0 - t) * (1.0 - t)) * p1 + 2.0 * (1.0 - t) * t * c + (t * t) * p2
				draw_circle(pos, radius, Color.WHITE)
			if loop_reveal and reveal_counter >= approx_len:
				reveal_counter = 0.0
		else:
			# 通过中点 + 法线偏移确定圆心
			var mid: Vector2 = (p1 + p2) * 0.5
			var n: Vector2 = Vector2(-dir.y, dir.x).normalized()
			var center: Vector2 = mid + n * curvature
			var r: float = center.distance_to(p1)
			# 计算起止角度，并选择较短的弧段
			var a1: float = (p1 - center).angle()
			var a2: float = (p2 - center).angle()
			var delta: float = a2 - a1
			delta = fmod(delta + PI, PI * 2.0) - PI
			# 用白点采样弧线而非连续线条，且按动画进度限制显示
			var arc_len: float = abs(delta) * r
			var steps: int = max(1, int(arc_len / spacing))
			var visible_len: float = min(reveal_counter, arc_len)
			var visible_steps: int = min(steps, int(visible_len / spacing))
			for i in range(visible_steps + 1):
				var t: float = float(i) / float(steps)
				var angle: float = a1 + delta * t
				var pos: Vector2 = center + Vector2(cos(angle), sin(angle)) * r
				draw_circle(pos, radius, Color.WHITE)
			if loop_reveal and reveal_counter >= arc_len:
				reveal_counter = 0.0
	else:
		# 直线白点：按固定间距采样
		var u: Vector2 = dir / length
		var count: int = int(length / spacing)
		var visible_len: float = min(reveal_counter, length)
		var visible_steps: int = min(count, int(visible_len / spacing))
		for i in range(visible_steps + 1):
			var pos: Vector2 = p1 + u * (i * spacing)
			draw_circle(pos, radius, Color.WHITE)
		if loop_reveal and reveal_counter >= length:
			reveal_counter = 0.0
